<?php
/**
 * Created by PhpStorm.
 * User: 火柴
 * Date: 2016/5/28
 * GitHub: https://github.com/huaiguoguo
 * Email: 290559038@qq.com
 * Time: 22:47
 */

namespace frontend\models;

use yii;
use yii\base\Model;
use yii\base\InvalidParamException;
use common\models\User;

/**
 * Password reset form
 */
class ChangePasswordForm extends Model
{
    public $password;
    public $new_password;
    public $re_password;

    /**
     * @var \common\models\User
     */
    public $_user;


    /**
     * Creates a form model given a token.
     *
     * @param  string                          $token
     * @param  array                           $config name-value pairs that will be used to initialize the object properties
     * @throws \yii\base\InvalidParamException if token is empty or not valid
     */
    public function __construct($config=[])
    {
        $uid = Yii::$app->user->identity->getId();
        $this->_user = User::findIdentity($uid);
        parent::__construct($config);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['password', 'new_password', 're_password'], 'required', 'message'=>'请填写 {attribute}'],
            [['password', 'new_password', 're_password'], 'string', 'max'=>20, 'min' => 6, 'tooLong'=>'请控制在6-20个字符之间', 'tooShort'=>'请控制在6-20个字符之间'],
        ];
    }


    public function attributeLabels()
    {
        return [
            'password' => '原密码',
            'new_password' => '新密码',
            're_password' => '重复新密码',
        ];
    }


    /**
     * Resets password.
     *
     * @return boolean if password was reset.
     */
    public function resetPassword()
    {
        $user = $this->_user;
        $user->setPassword($this->new_password);
        $user->removePasswordResetToken();

        return $user->save(false);
    }





}
